package com.smooware.icms.sync;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

/**
 * Forces app installing process by sending special intent to system. User is
 * asked if he wants to proceed install process and allowed to decline it.
 * 
 * @author Orest Vovchak
 */
public class AppInstaller {

	private static final String TAG = AppInstaller.class.getSimpleName();

	private static final String FILE_PREFIX = "file://";

	private static final String INSTALL_MIME_TYPE = "application/vnd.android.package-archive";

	private final Context context;

	public AppInstaller(final Context context) {
		this.context = context;
	}

	/**
	 * Forces apk installing process.
	 * 
	 * @param path
	 *            path to apk on SD card.
	 */
	public void installApp(final String path) {
		Log.d(TAG, path);

		Intent intent = new Intent(android.content.Intent.ACTION_VIEW);

		String fileAbsPath = FILE_PREFIX + path;

		intent.setDataAndType(Uri.parse(fileAbsPath), INSTALL_MIME_TYPE);

		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		context.startActivity(intent);
	}
}
